<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点名称" prop="siteName">
        <el-input v-model="queryParams.siteName" placeholder="请输入站点名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属系统ID" prop="systemId">
        <el-input v-model="queryParams.systemId" placeholder="请输入所属系统ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="区域ID" prop="regionId">
        <el-input v-model="queryParams.regionId" placeholder="请输入区域ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="站点地址" prop="siteAddress">
        <el-input v-model="queryParams.siteAddress" placeholder="请输入站点地址" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="queryParams.latitude" placeholder="请输入纬度" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="queryParams.longitude" placeholder="请输入经度" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input v-model="queryParams.contactPerson" placeholder="请输入联系人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="联系邮箱" prop="contactEmail">
        <el-input v-model="queryParams.contactEmail" placeholder="请输入联系邮箱" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input v-model="queryParams.creator" placeholder="请输入创建人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="上次更新时间" style="width: 308px">
        <el-date-picker v-model="daterangeLastUpdateTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updater">
        <el-input v-model="queryParams.updater" placeholder="请输入更新人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['site:basicSiteInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['site:basicSiteInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['site:basicSiteInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['site:basicSiteInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basicSiteInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点编号" align="center" prop="siteId" />
      <el-table-column label="站点名称" align="center" prop="siteName" />
      <el-table-column label="站点类型" align="center" prop="siteType" />
      <el-table-column label="所属系统ID" align="center" prop="systemId" />
      <el-table-column label="区域ID" align="center" prop="regionId" />
      <el-table-column label="站点地址" align="center" prop="siteAddress" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="联系邮箱" align="center" prop="contactEmail" />
      <el-table-column label="站点状态" align="center" prop="siteStatus" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="上次更新时间" align="center" prop="lastUpdateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['site:basicSiteInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['site:basicSiteInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改站点信息;对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="basicSiteInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点名称" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="所属系统ID" prop="systemId">
          <el-input v-model="form.systemId" placeholder="请输入所属系统ID" />
        </el-form-item>
        <el-form-item label="区域ID" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入区域ID" />
        </el-form-item>
        <el-form-item label="站点地址" prop="siteAddress">
          <el-input v-model="form.siteAddress" placeholder="请输入站点地址" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="联系邮箱" prop="contactEmail">
          <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="上次更新时间" prop="lastUpdateTime">
          <el-date-picker clearable v-model="form.lastUpdateTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择上次更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BasicSiteInfo">
import { listBasicSiteInfo, getBasicSiteInfo, delBasicSiteInfo, addBasicSiteInfo, updateBasicSiteInfo } from "@/api/basic/site/basicSiteInfo";

const { proxy } = getCurrentInstance();

const basicSiteInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);
const daterangeLastUpdateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    siteName: null,
    siteType: null,
    systemId: null,
    regionId: null,
    siteAddress: null,
    latitude: null,
    longitude: null,
    contactPerson: null,
    contactPhone: null,
    contactEmail: null,
    siteStatus: null,
    creator: null,
    lastUpdateTime: null,
    updater: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询站点信息;列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  if (null != daterangeLastUpdateTime && '' != daterangeLastUpdateTime) {
    queryParams.value.params["beginLastUpdateTime"] = daterangeLastUpdateTime.value[0];
    queryParams.value.params["endLastUpdateTime"] = daterangeLastUpdateTime.value[1];
  }
  listBasicSiteInfo(queryParams.value).then(response => {
    basicSiteInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    siteId: null,
    siteName: null,
    siteType: null,
    systemId: null,
    regionId: null,
    siteAddress: null,
    latitude: null,
    longitude: null,
    contactPerson: null,
    contactPhone: null,
    contactEmail: null,
    siteStatus: null,
    createTime: null,
    creator: null,
    lastUpdateTime: null,
    updater: null,
    remark: null
  };
  proxy.resetForm("basicSiteInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  daterangeLastUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.siteId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加站点信息;";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _siteId = row.siteId || ids.value
  getBasicSiteInfo(_siteId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改站点信息;";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["basicSiteInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.siteId != null) {
        updateBasicSiteInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBasicSiteInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _siteIds = row.siteId || ids.value;
  proxy.$modal.confirm('是否确认删除站点信息;编号为"' + _siteIds + '"的数据项？').then(function () {
    return delBasicSiteInfo(_siteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/basic/site/basicSiteInfo/export', {
    ...queryParams.value
  }, `basicSiteInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
